package com.marcosnobrega.android.threadservice;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.marcosnobrega.android.activity.AndroidNetwork;

public final class PresenceThread implements Runnable{
	
	private String android_id;
	private String nickname;
	static final PresenceThread presenceThread = new PresenceThread();
	
	public static final PresenceThread getInstance(String android_id,String nickname){
		presenceThread.android_id = android_id;
		presenceThread.nickname = nickname;
		return presenceThread;
	}
	
	private PresenceThread(){}

	@Override
	public void run() {
		
		while(true){
			try {
				pingStatus();
				Thread.sleep(180000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void pingStatus() throws Exception{
    	DefaultHttpClient client = new DefaultHttpClient();
    	
        HttpPost post = new HttpPost(AndroidNetwork.getServiceUrl()+"?action="+AndroidNetwork.getActionPingStatus());
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("userAndroidId", this.android_id));
        list.add(new BasicNameValuePair("nickname",this.nickname));
        
    	post.setEntity(new UrlEncodedFormEntity(list));
		client.execute(post);
    }

}
